package com.kerwin.project.videolist.controller;


import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kerwin.project.base.ProjectBaseService;
import com.kerwin.project.videolist.entity.KVideo;
import com.kerwin.project.videolist.service.VideoService;

/**
 * 对Video相关操作的Controller
 * @author KERWIN
 *
 */
@Controller()
@RequestMapping(value = "/videoList")
public class VideoListController {
	Logger logger = Logger.getLogger(VideoListController.class);
	
	@Resource(name = "videoService")
	private VideoService videoService;
	
	@Resource(name = "projectBaseService")
	private ProjectBaseService projectBaseService;

	/**
	 * 跳转到首页
	 * @return
	 */
	@RequestMapping("/list.do")
	public ModelAndView videoList(){
		List<KVideo> videoList = videoService.findVideoList();
		List<KVideo> videoDownloadList = videoService.findVideoDownloadList(0, 10);
		Map<String, List<KVideo>> map = new HashMap<String, List<KVideo>>();
		map.put("videoList", videoList);
		map.put("videoDownloadList", videoDownloadList);
		return new ModelAndView("video/videoList", map);
	}
	
	/**
	 * 跳转到添加Video页面
	 * @return
	 */
	@RequestMapping("/toAddVideo.do")
	public ModelAndView toAddVideo(){
		List<KVideo> videoList = videoService.findVideoList();
		List<KVideo> videoDownloadList = videoService.findVideoDownloadList(0, 10);
		Map<String, List<KVideo>> map = new HashMap<String, List<KVideo>>();
		map.put("videoList", videoList);
		map.put("videoDownloadList", videoDownloadList);
		return new ModelAndView("video/addVideo", map);
	}
	
	/**
	 * 保存video
	 * @param video
	 * @param writer
	 */
	@RequestMapping("/addVideo.do")
	public void addVideo(HttpServletRequest request, PrintWriter writer){
		KVideo video = new KVideo();
		String path = "/images/video/videoPoster";
		String realPath = request.getServletContext().getRealPath(path);
		video = this.projectBaseService.uploadFile(request, realPath, path);
		if(null != video){
			boolean flag = videoService.addVideo(video);
			if(flag){
				writer.write("true");
			}else{
				writer.write("false");
			}
		}else{
			writer.write("false");
		}
	}
	
	/**
	 * 跳转到video详情页面
	 * @param videoId
	 */
	@RequestMapping("/toVideoDetail.do")
	public ModelAndView toVideoDetail(String videoId){
		KVideo video = this.videoService.findVideoById(Long.parseLong(videoId));
		List<KVideo> videoDownloadList = videoService.findVideoDownloadList(0, 10);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("video", video);
		map.put("videoDownloadList", videoDownloadList);
		return new ModelAndView("video/videoDetail", map);
	}
}
